package com.tianpengtech.common.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.kit.HashKit;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.account.Account;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

public class XxxUtil {

    /*
     * 统一获取指定前缀的对应类的数据表名称
     */
    public static String tableName(String prefix, Class clazz) {

        String tableName = prefix + clazz.getSimpleName().toLowerCase();
        return tableName;
    }

    /*
     * 统一获取指定前缀为t_的类的数据表名称
     */
    public static String tableName(Class clazz) {
        String DEFAULT_PREFIX = "t_";
        return tableName(DEFAULT_PREFIX, clazz);
    }

    /*
     * 计算页码数
     */
    public static int calculatePageNumForDataTable(int start, int offset) {

        start = start / offset;
        start = (start == 0) ? 1 : start + 1;
        return start;
    }

    /*
     * 创建Map
     */
    public static Map<String, Object> createMapInstance() {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /*
     * 	将字符串变为字符串数组
     */
    public static String[] splitStr(String v) {
        String result[] = {"0", "未知"};
        return (v == null || StringUtils.isEmpty(v)) ? result : v.split(",");
    }

    public static void log(String s) {
        System.out.println("**********************");
        System.out.println(s);
        System.out.println("**********************");
    }


    public static long fenToYuan(long yuan) {

        return yuan * 100;
    }

    public static double yuanTofen(long fen) {

        return (fen * 1.0) / 100;
    }


    /*
     * 下划线转驼峰
     * SmallCamel 是否首字母小写
     */
    public static String underline2Camel(String line, boolean smallCamel) {
        if (line == null || "".equals(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /*
     * 驼峰转下划线
     */
    public static String camel2Underline(String line) {
        if (line == null || "".equals(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }


    public static String passMd5(String pass){
        return HashKit.md5(pass + GlobalConfigConstant.SALT_KEY);
    }

    /**
     * 	读取Session信息中的登录信息
     * @return
     */
    public static Account readSessionInfo(){
    	Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		Account account = (Account)session.getAttribute(GlobalConfigConstant.SESSION_KEY_ACCOUNT);
		return account;
    }
    
    
    
    public static String arr2str(String[] arr){
    	StringBuffer sb = new StringBuffer();
		if(arr!=null){
			for (int i = 0; i < arr.length; i++) {
				if(i<arr.length-1){
					sb.append(arr[i]);
					sb.append(",");
				}else{
					sb.append(arr[i]);
				}
			}	
		}
		return sb.toString();
	}
}
